package com.linkdood.app.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.promeg.pinyinhelper.Pinyin;
import lombok.extern.slf4j.Slf4j;

/**
 * 拼音工具类
 * @author YunJ
 */
@Slf4j
public class PinYinUtil {

    private static final Character LOWER_CASE_A = 'a';
    private static final Character UPPER_CASE_A = 'A';
    private static final Character LOWER_CASE_Z = 'z';
    private static final Character UPPER_CASE_Z = 'Z';

    public static String getFirstPinyin(String name) {
        String res = "";

        if (StringUtils.isBlank(name)) {
            return res;
        }

        try {
            char first = name.charAt(0);
            String pinyin = Pinyin.toPinyin(first);
            char c = pinyin.charAt(0);
            boolean isChar = (c >= LOWER_CASE_A && c <= LOWER_CASE_Z) || (c >= UPPER_CASE_A && c <= UPPER_CASE_Z);
            if (isChar) {
                return String.valueOf(c).toUpperCase();
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
        return res;
    }
}
